package cn.wolfcode.trip.base.service.impl;
import cn.wolfcode.trip.base.domain.Collect;
import cn.wolfcode.trip.base.domain.SystemInfo;
import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.TravelContent;
import cn.wolfcode.trip.base.mapper.CollectMapper;
import cn.wolfcode.trip.base.mapper.SystemInfoMapper;
import cn.wolfcode.trip.base.mapper.TravelContentMapper;
import cn.wolfcode.trip.base.mapper.TravelMapper;
import cn.wolfcode.trip.base.query.CollecQueryObject;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.util.SystemInfoUtils;
import cn.wolfcode.trip.base.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Date;
import java.util.List;

/**
 * Copyright: Copyright (c) 2019 Super520
 * 描述：我的游记
 *
 * @package: cn.wolfcode.trip.base.service.impl
 * @author: Patch
 * @date: 2019/5/21 16:45
 * 总结:
 */
@Service
public class TravelServiceImpl implements ITravelService {
    @Autowired
    private TravelMapper travelMapper;
    @Autowired
    private TravelContentMapper travelContentMapper;
    @Autowired
    private CollectMapper collectMapper;
    @Autowired
    private  SystemInfoUtils systemInfoUtils;
    @Override
    public PageInfo<Travel> query(QueryObject qo) {
        // 利用第四个参数，取消合理化分页
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), true, false, null);
        List<Travel> list = travelMapper.selectForList(qo);
        return new PageInfo<>(list);
    }

    @Override
    public void saveOrUpdate(Travel travel) {
        // 保存游记内容
        TravelContent content = travel.getTravelContent();
        // 最后更新时间
        travel.setLastUpdateTime(new Date());
        if (travel.getId() != null) {
            // 更新
            travelMapper.updateByPrimaryKey(travel);
            // 跟新游记内容
            travelContentMapper.updateByPrimaryKey(content);
        } else {
            // 创建时间
            travel.setCreateTime(new Date());
            // 新增
            travelMapper.insert(travel);
            // 保存游记id，主键关联
            content.setId(travel.getId());
            // 新增游记内容
            travelContentMapper.insert(content);
        }
    }

    @Override
    public Travel get(Long id) {
        return travelMapper.selectByPrimaryKey(id);
    }

    @Override
    public Travel getContentById(Long id) {
        return travelMapper.selectByPrimaryKey(id);
    }

    @Override
    public void updateState(Travel travel) {
        // 如果是发布状态就需要设置发布时间和通知作者
        if (travel.getState() == Travel.STATE_RELEASE) {
            systemInfoUtils.getReleaseString(travel);//发布通知
            travel.setReleaseTime(new Date());
        }
        travelMapper.updateState(travel);
    }

    @Override
    public PageInfo<Travel> listContentByKeyword(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Travel> list = travelMapper.selectContentByKeyword(qo);
        return new PageInfo<Travel>(list);
    }


    @Override
    public PageInfo<Travel> selectCollectTravel(Collect collect, CollecQueryObject qo) {
        //设置当前用户
        collect.setUser(UserContext.getCurrentUser());
        //先查询出该用户下所有的收藏内容的id
       List<Integer> collectId = collectMapper.selectCollectId(collect);
      // PageHelper.startPage(1,3);
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
       List<Travel> travels = travelMapper.selectByCollectId(collectId);
       return new PageInfo<>(travels);
    }


}